﻿//------------------------------------------------------------------------------
// 创建标识: Copyright (C) 2008 Socansoft.com 版权所有
// 创建描述: SocanCode代码生成器自动创建于 2008-8-13 17:43:17
//
// 功能描述: 
//
// 修改标识: 
// 修改描述: 
//------------------------------------------------------------------------------

using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using IDAL;
using DBUtility;
using System.Collections;
using System.Collections.Generic;

namespace SQLServerDAL
{
    /// <summary>
    /// 数据访问类 FUNCTION_OPERATION
    /// </summary>
    public class FUNCTION_OPERATION : IFUNCTION_OPERATION
    {
        public FUNCTION_OPERATION()
        { }

        #region  成员方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public string Add(Model.FUNCTION_OPERATION model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [FUNCTION_OPERATION](");
            strSql.Append("[FO_FUNCCODE],[FO_OPCODE])");
            strSql.Append(" values (");
            strSql.Append("@FO_FUNCCODE,@FO_OPCODE)");
            SqlParameter[] parameters = {
                    new SqlParameter("@FO_FUNCCODE", SqlDbType.VarChar,15),
                    new SqlParameter("@FO_OPCODE", SqlDbType.VarChar,15)
            };
            parameters[0].Value = model.FO_FUNCCODE;
            parameters[1].Value = model.FO_OPCODE;

            if (SqlHelper.ExecuteSql(SqlHelper.LocalSqlServer, strSql.ToString(), parameters) > 0)
                   return model.FO_FUNCCODE;
            else
                return null;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.FUNCTION_OPERATION model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [FUNCTION_OPERATION] set ");
            strSql.Append("[FO_OPCODE]=@FO_OPCODE");
            strSql.Append(" where [FO_FUNCCODE]=@FO_FUNCCODE");
            SqlParameter[] parameters = {
                    new SqlParameter("@FO_FUNCCODE", SqlDbType.VarChar,15),
                    new SqlParameter("@FO_OPCODE", SqlDbType.VarChar,15)
            };
            parameters[0].Value = model.FO_FUNCCODE;
            parameters[1].Value = model.FO_OPCODE;

            return SqlHelper.ExecuteSql(SqlHelper.LocalSqlServer, strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string FO_FUNCCODE)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete [FUNCTION_OPERATION] ");
            strSql.Append(" where [FO_FUNCCODE]=@FO_FUNCCODE");
            SqlParameter[] parameters = {
                    new SqlParameter("@FO_FUNCCODE", SqlDbType.VarChar,15)};
            parameters[0].Value = FO_FUNCCODE;

            return SqlHelper.ExecuteSql(SqlHelper.LocalSqlServer, strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string FO_FUNCCODE)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [FUNCTION_OPERATION]");
            strSql.Append(" where [FO_FUNCCODE]= @FO_FUNCCODE");
            SqlParameter[] parameters = {
                    new SqlParameter("@FO_FUNCCODE", SqlDbType.VarChar,15)};
            parameters[0].Value = FO_FUNCCODE;
            return SqlHelper.Exists(SqlHelper.LocalSqlServer, strSql.ToString(), parameters);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.FUNCTION_OPERATION GetModel(string FO_FUNCCODE)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from [FUNCTION_OPERATION] ");
            strSql.Append(" where [FO_FUNCCODE]=@FO_FUNCCODE");
            SqlParameter[] parameters = {
                    new SqlParameter("@FO_FUNCCODE", SqlDbType.VarChar,15)};
            parameters[0].Value = FO_FUNCCODE;
            DataSet ds = SqlHelper.Query(SqlHelper.LocalSqlServer, strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow r = ds.Tables[0].Rows[0];
                return GetModel(r);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取数据集,建议只在多表联查时使用
        /// </summary>
        public DataSet GetDataSet(string strSql)
        {
            return SqlHelper.Query(SqlHelper.LocalSqlServer, strSql);
        }

        /// <summary>
        /// 获取泛型数据列表,建议只在多表联查时使用
        /// </summary>
        public IList<Model.FUNCTION_OPERATION> GetList(string strSql)
        {
            return GetList(GetDataSet(strSql));
        }

        /// <summary>
        /// 获取数据集,在单表查询时使用
        /// </summary>
        public DataSet GetDataSet(string strWhat, string strWhere, string strOrderby)
        {
            if (string.IsNullOrEmpty(strWhat))
                strWhat = "*";
            StringBuilder strSql = new StringBuilder("select " + strWhat + " from [FUNCTION_OPERATION]");
            if (!string.IsNullOrEmpty(strWhere))
                strSql.Append(" where " + strWhere);
            if (!string.IsNullOrEmpty(strOrderby))
                strSql.Append(" order by " + strOrderby);
            return SqlHelper.Query(SqlHelper.LocalSqlServer, strSql.ToString());
        }

        /// <summary>
        /// 获取泛型数据列表,在单表查询时使用
        /// </summary>
        public IList<Model.FUNCTION_OPERATION> GetList(string strWhat, string strWhere, string strOrderby)
        {
            return GetList(GetDataSet(strWhat, strWhere, strOrderby));
        }

        /// <summary>
        /// 分页获取泛型数据列表
        /// </summary>
        public IList<Model.FUNCTION_OPERATION> GetList(int pageSize, int pageIndex, string fldSort, bool sort, string strCondition, out int pageCount, out int count)
        {
            string strSql;
            DataSet ds = SqlHelper.PageList(SqlHelper.LocalSqlServer, "[FUNCTION_OPERATION]", null, pageSize, pageIndex, fldSort, sort, strCondition, "FO_FUNCCODE", false, out pageCount, out count, out strSql);
            return GetList(ds);
        }
        #endregion

        /// <summary>
        /// 由一行数据得到一个实体
        /// </summary>
        private Model.FUNCTION_OPERATION GetModel(DataRow r)
        {
            Model.FUNCTION_OPERATION model = new Model.FUNCTION_OPERATION();
            model.FO_FUNCCODE = SqlHelper.GetString(r["FO_FUNCCODE"]);
            model.FO_OPCODE = SqlHelper.GetString(r["FO_OPCODE"]);
            return model;
        }

        /// <summary>
        /// 由数据集得到泛型数据列表
        /// </summary>
        private IList<Model.FUNCTION_OPERATION> GetList(DataSet ds)
        {
            List<Model.FUNCTION_OPERATION> l = new List<Model.FUNCTION_OPERATION>();
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                l.Add(GetModel(r));
            }
            return l;
        }
    }
}
